﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OpenPop.Pop3;

public partial class ReadMail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        
        
    }
    protected void gvEmails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string DateSend = HttpUtility.UrlEncode(((System.Web.UI.HtmlControls.HtmlInputHidden)e.Row.FindControl("DateSent_hidden")).Value);
            string From = HttpUtility.UrlEncode(((System.Web.UI.HtmlControls.HtmlInputHidden)e.Row.FindControl("From_hidden")).Value);
            e.Row.Attributes.Add("ondblclick", "javascript:return GetCellValue('" + "DateSent=" + DateSend + "&From=" + From + "');");
            
            
            e.Row.Attributes.Add("onclick", "javascript:return quickread('" + e.Row.FindControl("body").ClientID + "');");
            Image att = (Image)e.Row.FindControl("attach");
            //e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvEmails, "Select$" + e.Row.RowIndex);
            //if (((DataRowView)e.Row.DataItem).Row[5].ToString() != "0")
            //{

            //    att.ImageUrl = "~/icons/Attachment2.png";
            //}
            //else
            //{
            //    att.Visible = false;

            //}
        }


    }
    protected void selectall_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow dr in gvEmails.Rows)
        {

            CheckBox chk = (CheckBox)dr.FindControl("chkAdd");
            chk.Checked = true;

        }

    }
    protected void deselectall_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow dr in gvEmails.Rows)
        {

            CheckBox chk = (CheckBox)dr.FindControl("chkAdd");
            chk.Checked = false;

        }
    }
    
    mypopclient pop3Client = new mypopclient();
    
   
    protected void delete_Click(object sender, EventArgs e)
    {
        DataTable table = pop3Client.GetAllMesseges();
        List<int> deletedIds = new List<int>();
        foreach (GridViewRow dr in gvEmails.Rows)
        {
            
            CheckBox chk = (CheckBox)dr.FindControl("chkAdd");
            if (chk.Checked)
            {
                System.Web.UI.HtmlControls.HtmlInputHidden From_h = (System.Web.UI.HtmlControls.HtmlInputHidden)dr.FindControl("From_hidden");
                System.Web.UI.HtmlControls.HtmlInputHidden Subject_h = (System.Web.UI.HtmlControls.HtmlInputHidden)dr.FindControl("Subject_hidden");
                System.Web.UI.HtmlControls.HtmlInputHidden DateSent_h = (System.Web.UI.HtmlControls.HtmlInputHidden)dr.FindControl("DateSent_hidden");
                System.Web.UI.HtmlControls.HtmlInputHidden Body_h = (System.Web.UI.HtmlControls.HtmlInputHidden)dr.FindControl("Body_hidden");
                foreach (DataRow row in table.Rows)
                {
                    if (row["From"].ToString() == HttpUtility.HtmlDecode(From_h.Value.ToString()) && row["Subject"].ToString() == HttpUtility.HtmlDecode(Subject_h.Value.ToString()) && row["DateSent"].ToString() == HttpUtility.HtmlDecode(DateSent_h.Value.ToString()))
                    {
                        int Messegeid = int.Parse(row["MessageNumber"].ToString());
                        mypopclient pop1 = new mypopclient();
                        pop1.Connect();
                        pop1.DeleteMessage(Messegeid);
                        pop1.Disconnect();
                        deletedIds.Add(Messegeid);
                    
                    }
                }
               

             
             
            }
        }
        foreach (int i in deletedIds)
        {
            int count = table.Rows.Count;
            for (int x = 0; x < count; x++)
            {
                if (i == int.Parse(table.Rows[x]["MessageNumber"].ToString()))
                {
                    table.Rows.Remove(table.Rows[x]);
                    break;
                }
            }
        }
        

        gvEmails.DataSource = null;
        gvEmails.DataSource = table;
        
        
        gvEmails.DataBind();


     
       
    }
    protected void gvEmails_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.cursor='pointer'");

            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='default'");

        }
    }
}
